package earogya.pharmacy.patient.messages;

import java.io.IOException;
import java.sql.Time;

import org.hibernate.HibernateException;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.v251.message.RDS_O13;
import ca.uhn.hl7v2.model.v251.segment.ORC;
import ca.uhn.hl7v2.model.v251.segment.RXD;
import earogya.pharmacy.patient.*;

/* RDS^O13 (Pharmacy treatment dispence) message handler
 * 
 * reads the RDs^O13 message and extract data from it and
 * sends the prescribed drug details to the dispenced_drugs database using hibernate
 *
 **/
public class RDS_O13_Handler {
	
	
	private RDS_O13 adrMsg;
	private PatientHibernateManager hibernate_manager=new PatientHibernateManager();
	
	private Dispenced_Treatments dispence = new Dispenced_Treatments();
	public RDS_O13_Handler(RDS_O13 adrMsg){
		this.adrMsg=adrMsg;
	}
	
	public String manageMessage() throws HL7Exception, IOException{
		String ack = null;
		try{
				this.updateDrugDispenceDetails();
				ack=adrMsg.generateACK().encode();
		}
		catch(HL7Exception e){
			ack = adrMsg.generateACK("OE", e).encode();
		}
		return ack;
	}
	
	public void updateDrugDispenceDetails() throws HL7Exception, HibernateException{
		
		RXD rxd = adrMsg.getORDER().getRXD();
		ORC orc = adrMsg.getORDER().getORC();
		
		dispence.setDispenceId(Integer.parseInt(orc.getOrc2_PlacerOrderNumber().getEi1_EntityIdentifier()
				.getValue()));
		
		if(adrMsg.getPATIENT().getPID().getPatientID().getCx1_IDNumber().getValue()!= null){
			dispence.setPatientId(Integer.parseInt(adrMsg.getPATIENT().getPID().getPatientID().getCx1_IDNumber().getValue()));
		}
		
		if(rxd.getDispenseGiveCode().getCe1_Identifier().getValue()!=null){
			dispence.setDrugId(Integer.parseInt(rxd.getDispenseGiveCode().getCe1_Identifier().getValue()));
		}
		if(rxd.getRxd1_DispenseSubIDCounter().getValue()!= null){
			dispence.setNoOfIssues(Integer.parseInt(rxd.getRxd1_DispenseSubIDCounter().getValue()));
		}
		if(rxd.getRxd6_ActualDosageForm().getCe1_Identifier().getValue()!= null){
			dispence.setDosage(rxd.getRxd6_ActualDosageForm().getCe1_Identifier().getValue());
		}
		if(rxd.getRxd4_ActualDispenseAmount().getValue()!=null){
			dispence.setDispencedQuantity(Integer.parseInt(rxd.getRxd4_ActualDispenseAmount().getValue()));
		}
		if(rxd.getRxd5_ActualDispenseUnits().getCe1_Identifier().getValue()!= null){
			dispence.setDispenceUnits(rxd.getRxd5_ActualDispenseUnits().getCe1_Identifier().getValue());
		}
		if (orc.getDateTimeOfTransaction().getTime().getValueAsDate()!= null) {
			dispence.setDispenceDate(orc.getDateTimeOfTransaction().getTime()
					.getValueAsDate());
			dispence.setDispenceTime(this.toTime());
		}
		if(rxd.getRxd7_PrescriptionNumber().getValue()!=null){
			dispence.setPrescriptionNo(Integer.parseInt(rxd.getRxd7_PrescriptionNumber().getValue()));
		}
		if(rxd.getRxd10_DispensingProvider(0).getIDNumber().getValue()!= null){
			dispence.setDispencedById(Integer.parseInt(rxd.getRxd10_DispensingProvider(0).getIDNumber().getValue()));
		}
		hibernate_manager.saveObject(dispence);
	}
	
	@SuppressWarnings("deprecation")
	public Time toTime() throws DataTypeException {
		int hh = 0, mm = 0, ss = 0;
		RXD rxd = adrMsg.getORDER().getRXD();
		try {
			hh = rxd.getRxd3_DateTimeDispensed().getTime().getHour();
			mm = rxd.getRxd3_DateTimeDispensed().getTime().getMinute();
			ss = rxd.getRxd3_DateTimeDispensed().getTime().getSecond();
		} catch (DataTypeException e) {
		}
		Time time = new Time(hh, mm, ss);
		return time;
	}

}
